package com.szmirren.entity.ams;

import java.sql.Types;
import java.util.*;

/**
 * @author Lin
 * @date 2018/5/29
 */
public class TableBOInfo {
    private String tableName;
    private List<Column> columns;
    private Map<String, Column> columnMap;
    private String[] primaryKeys;
    private List<Column> splitCols = new ArrayList<>();

    public TableBOInfo(String tableName, List<Column> columns, Set<String> primaryKeys, List<String> splitCols) {
        this.tableName = tableName;
        this.columns = columns;
        this.columnMap = new HashMap<>();
        for (Column column : columns) {
            this.columnMap.put(column.getName(), column);
        }
        this.primaryKeys = primaryKeys == null ? new String[0] : primaryKeys.toArray(new String[]{});
        if (splitCols != null) {
            Set<String> cols = new HashSet<>();
            for (String splitCol : splitCols) {
                if (cols.add(splitCol) && !columnMap.containsKey(splitCol)) {
                    Column col = new Column(splitCol, Types.VARCHAR, "VARCHAR", 8, 0, "", false, false);
                    this.splitCols.add(col);
                }
            }
        }
    }

    public String getTableName() {
        return tableName;
    }

    public List<Column> getColumns() {
        return columns;
    }

    public String getMainAttribute() {
            return primaryKeys.length == 1 ? primaryKeys[0] : "";
    }

    public List<Column> getSplitCols() {
        return splitCols;
    }

    public String getType() {
        return "table";
    }
}
